﻿using ConsoleApp1.Models.DB;
using ConsoleApp1.Uitls;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.Services
{
    public class DataUploadOutPartailService : IDataUploadService<TOutPartial>
    {



        private PrintService printService;

        private TOutPartial lastData;

        private FreeSqlService freeSqlService;

        private MyHttpHelper myHttpHelper = new MyHttpHelper("http://192.168.1.11:8581/api/v1");


        public DataUploadOutPartailService(PrintService printService,FreeSqlService freeSqlService)
        {
            this.printService = printService;
            this.freeSqlService = freeSqlService;
            this.printService.SetTitle("外置局放");
        }

        public void Init()
        {
            printService.Debug("读取");
            lastData = freeSqlService.From
                .Select<TOutPartial>()
                .OrderByDescending(t => t.Id)
                .First();
            
            if (lastData == null)
            {
                printService.Error("数据库为空");
            }
            else
            {
                printService.Debug($"最新的Id为{lastData.Id}");
                printService.Debug("发送最新一条数据用于测试");
                Send(lastData);
            }

        }

        public void LoopSend()
        {
            var lists = freeSqlService.From.Select<TOutPartial>()
                .Where(t => t.Id > lastData.Id)
                .OrderByDescending(t => t.Id)
                .ToList();
            printService.Debug($"查询到{lists.Count}个新增数据");

            foreach (var item in lists)
            {
                Send(item);
            }
            lastData = freeSqlService.From
                .Select<TOutPartial>()
                .OrderByDescending(t => t.Id)
                .First();
            printService.Info($"最新Id为{lastData.Id}");
        }

        public void Send(TOutPartial data)
        {
            var api = "uploadJFValue";
            printService.Debug($"{data.TerminalId}发送到泰岳{api}");
            var sendData = new Rootobject()
            {
                terminalId = data.TerminalId,
                aFrequency = data.AFrequency,
                aMaxElectric = data.AMaxElectric.ToString(),
                aMaxFrequency = data.AMaxFrequency.ToString(),
                aElectricArray = data.AElectric,
                astatus = data.Astatus.ToString(),

                bFrequency = data.BFrequency,
                bMaxElectric = data.BMaxElectric.ToString(),
                bMaxFrequency = data.BMaxFrequency.ToString(),
                bElectricArray = data.BElectric,
                bstatus = data.Bstatus.ToString(),


                cFrequency = data.CFrequency,
                cMaxElectric = data.CMaxElectric.ToString(),
                cMaxFrequency = data.CMaxFrequency.ToString(),
                cElectricArray = data.CElectric,
                cstatus = data.Cstatus.ToString(),
            };
            printService.Debug(JsonConvert.SerializeObject(sendData));
            myHttpHelper.MyHttpPost(api, sendData, null);

        }


        public class Rootobject
        {
            public string terminalId { get; set; }
            public string aFrequency { get; set; }
            public string aMaxElectric { get; set; }
            public string aMaxFrequency { get; set; }
            public string aElectricArray { get; set; }
            public string bFrequency { get; set; }
            public string bMaxElectric { get; set; }
            public string bMaxFrequency { get; set; }
            public string bElectricArray { get; set; }
            public string cFrequency { get; set; }
            public string cMaxElectric { get; set; }
            public string cMaxFrequency { get; set; }
            public string cElectricArray { get; set; }
            public string astatus { get; set; }
            public string bstatus { get; set; }
            public string cstatus { get; set; }
        }

    }
}
